package com.ton.system.service;

import com.alibaba.fastjson.JSON;
import com.ton.system.entity.AdminPermission;
import com.ton.system.entity.AdminUser;
import com.ton.system.entity.AdminUserRole;
import com.ton.system.entity.LoginCenterConfigVo;
import com.ton.utils.SqlArgUtil;
import com.kypj.frame.common.EmptyUtil;
import com.kypj.frame.common.NumberUtil;
import com.kypj.frame.db.MyTemplate;
import com.kypj.frame.db.Pager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class AdminUserService {
    @Resource
    private MyTemplate myTemplate;

    public LoginCenterConfigVo getLoginCenterConfigVo() {
        String configValue = myTemplate.myGetString("select config_value from app_init_config where config_key=?",
                "login_center_config");
        if (EmptyUtil.isNotEmpty(configValue) && configValue.length() > 20) {
            return JSON.parseObject(configValue, LoginCenterConfigVo.class);
        }
        return null;
    }

    public List<AdminUser> listAdminUser(Pager pager, String name) {
        String search = SqlArgUtil.like(name);
        if (pager.getCount() == 0) {
            int count = myTemplate.myCountAppendWhereCondition(AdminUser.class, "flag!=9 and #login_name=? and #person_name=?", search, search);
            pager.setCount(count);
        }
        return myTemplate.myQueryForListAppendWhereCondition(AdminUser.class, "flag!=9 and #login_name=? and #person_name=? limit ?,?", search, search, pager.limitStart(), pager.limitEnd());
    }

    public List<AdminPermission> listAdminPermissionByUserId(Integer userId) {
        return myTemplate.myQueryForListAppendWhere(AdminPermission.class,
                "id in (SELECT a.permission FROM admin_role_permission a,admin_user_role b " +
                        "WHERE b.role_id=a.role_id and b.admin_user_id=?)", userId);
    }

    public void addAdminUser(AdminUser adminUser, String adminRoleIds) {
        myTemplate.mySaveObject(adminUser);
        if (EmptyUtil.isNotEmpty(adminRoleIds)) {
            String[] ids = adminRoleIds.split(",");
            for (String roleId : ids) {
                if (NumberUtil.isInteger(roleId)) {
                    AdminUserRole userRole = new AdminUserRole();
                    userRole.setAdminUserId(adminUser.getId());
                    userRole.setRoleId(Integer.parseInt(roleId));
                    myTemplate.mySaveObject(userRole);
                }
            }
        }
    }

    public void updateAdminUser(AdminUser adminUser) {
        myTemplate.myUpdateObject(adminUser);
    }

    public void updateAdminUser(AdminUser adminUser, String adminRoleIds) {
        myTemplate.myUpdateObject(adminUser);
        myTemplate.myUpdate("delete from admin_user_role where admin_user_id=?", adminUser.getId());
        if (EmptyUtil.isNotEmpty(adminRoleIds)) {
            String[] ids = adminRoleIds.split(",");
            for (String roleId : ids) {
                if (NumberUtil.isInteger(roleId)) {
                    AdminUserRole userRole = new AdminUserRole();
                    userRole.setAdminUserId(adminUser.getId());
                    userRole.setRoleId(Integer.parseInt(roleId));
                    myTemplate.mySaveObject(userRole);
                }
            }
        }
    }

    public void updateAdminUserFlag(Integer id, Integer flag) {
        AdminUser adminUser = new AdminUser();
        adminUser.setId(id);
        adminUser.setFlag(flag);
        myTemplate.myUpdateObjectSelective(adminUser);
    }

    public AdminUser findAdminUserByLoginName(String loginName) {
        return myTemplate.myGetObjectAppendWhere(AdminUser.class, "login_name=?", loginName);
    }

    public AdminUser findAdminUserById(Integer id) {
        return myTemplate.myGetObjectByPK(AdminUser.class, id);
    }

    public void clearDevice(Integer userId) {
        myTemplate.myUpdate("update admin_user set device_ids=null where id=?", userId);
    }
}
